


//
//  ZMoreVideoViewController.m
//  ZQLearn
//
//  Created by macfeel on 2019/5/9.
//  Copyright © 2019 macfeel. All rights reserved.
//

#import "ZMoreVideoViewController.h"
#import "VideoCell.h"
#import "ReadModel.h"
#import "ZVideoViewController.h"
#import "HomeModel.h"

@interface ZMoreVideoViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)NSMutableArray *dataArray;
@end

#define cellId @"video"

@implementation ZMoreVideoViewController
-(UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0 , 1, ScreenWidth , ScreenHeight -ScreenHeightFlex) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.rowHeight = 186.f;
        _tableView.backgroundColor = kBackgroundColor;
        [_tableView registerNib:[UINib nibWithNibName:@"VideoCell" bundle:nil] forCellReuseIdentifier:cellId];
        ///分割线
        _tableView.separatorStyle = 0;

//        WeakStament(ws);
        //        _tableView.mj_header = [CustomRefreshHeader headerWithRefreshingBlock:^{
        //            [ws refreshData];
        //        }];
        
        //        _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        //            [ws loadMoreData];
        //        }];
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.tableFooterView = [UIView new];
        
    }
    return _tableView;
}
- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    ///更多视频
    [self.navigationItem setItemWithTitle:self.ttitle?:@"更多视频" textColor:kNavigationTitleColor fontSize:18 itemType:center];
    self.dataArray = [NSMutableArray array];
    [self.view addSubview:self.tableView];
    [self loadData];
    // Do any additional setup after loading the view.
}

- (void)loadData{
    
    NSDictionary *dic;
    NSString *url;
    if ([self.isComicFlag isEqualToString:@"comic"]) {
        ///动漫馆 点进来也是视频列表
        url = kComicVideoListUrl;
        dic = @{@"pageIndex":@"1",@"pageSize":@"50",@"ComicColumnInfo.id":self.cateId?:@""};
    }else{
        
        //首页点击更对进来
        url = kMoreUrl;
        dic = @{@"pageIndex":@"1",@"pageSize":@"50",@"qualityEducationColumnInfo.id":self.cateId?:@""};
    }
    
    [ZNetworkService postRequestWithBaseUrl:url setParameters:dic success:^(id result) {
        
        YYLog(@"%@",result);
        
        if (result[@"errorCode"]) {
            NSArray *arr = result[@"result"];
            
            for (NSDictionary* dic in arr) {
                
                ReadModel *model = [ReadModel mj_objectWithKeyValues:dic];
                model.price = model.videoInfo[@"price"];
                model.ID = model.videoInfo[@"id"];
                NSString *vUrl = model.videoInfo[@"videoUrl"];
                model.videoUrl = vUrl;
//               NSArray *cArr =  [vUrl componentsSeparatedByString:@" "];
//                model.videoUrl = [cArr lastObject];
                [self.dataArray addObject:model];
                
            }
            
            [self.tableView reloadData];
        }
    } fail:^(NSError *error) {
        
    } delegater:nil];

}

#pragma mark - datasource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
//    return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ///扯淡，数组没有初始化，一直不走这个代理
    ReadModel *model = self.dataArray[indexPath.row];
    
    VideoCell *cell= [tableView dequeueReusableCellWithIdentifier:cellId forIndexPath:indexPath];
    cell.bottomBar.hidden = YES;
    cell.titleLab.text = model.videoInfo[@"name"];
    cell.bottomHeight.constant = 30.f;
    cell.leftMargin.constant = 15.f;
    cell.rightMargin.constant = 15.f;
    [cell.headImg sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",kPicServerUrl,model.videoInfo[@"photo"]]] placeholderImage:nil];
//    cell.titleLab.text = model.title;
//    cell.timeLab.text = model.updateTime;
    return cell;
    
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    ReadModel *model = self.dataArray[indexPath.row];
    MidModel *mModel = [MidModel new];
    mModel.name = model.videoInfo[@"name"];
    mModel.ID = model.ID;
    mModel.price = model.price;
    mModel.picUrl = [NSString stringWithFormat:@"%@%@",kPicServerUrl,model.videoInfo[@"photo"]];
    mModel.videoUrl = [NSString stringWithFormat:@"%@%@",kPicServerUrl,model.videoUrl];
    YYLog(@"%@",model.videoUrl);
    ZVideoViewController *videoDetail = [ZVideoViewController new];
    videoDetail.MModel = mModel;
    [self.navigationController pushViewController:videoDetail animated:YES];
}

@end
